/**
 * Created by Administrator on 2018/1/13.
 */
/**
 * Created by Administrator on 2017/12/2.
 */
define(function(require, exports, module) {
    var $=require('zepto');
    api=require('api');
    cn=require('conment');
    cook=require('cookie');
    module.exports = {
        int:{
            is:false,
            isApprove:false,
            isBindName:false,
            isBindBank:false,
            tels:''
        },
        isLogin:function () {
            const _this=this;
            cn.Ajax(api.isLogin,{
            },function(d){
                if(d.flag==1){
                    _this.isBindName=true;
                    _this.tels=d.username;
                    $('#setTel').text(d.username);
                }
            },'post')
        },


        bank_list:function () {
            const _this=this;
            cn.Ajax(api.bankcardList,{
            },function(d){
                if(d.flag==1){
                    if(d.list.length>0){
                       _this.isBindBank=true;
                        var l=d.list[0].no;
                        $('#cardId').val(d.list[0].id);
                        $('.card_no').text(l.substring(l.length-4,l.length));
                        $('#banklist').show();
                    }else{
                        $('#banklist').hide();
                    }
                }
            },'post')
        },
        p:{
            timeTxt:'发送验证码',
            time:60,
            istime:false,
        },
        getcodeTime:function (txt) {
            var _this=this
            if(!_this.p.istime) return;
            _this.p.time>0?$('#codeTxt').text(_this.p.time+'s'):$('#codeTxt').text('重新获取');
            --_this.p.time;
            _this.p.time<0?_this.p.istime=false:_this.p.istime=true
            setTimeout(function () {
                _this.getcodeTime()
            },1000);
        },
        rechargeData:null,
        reCharge:function () {
            var _this=this;
            var amounts=$('#account').val()
            var smCodes=$('#register-code').val()
            var telsInput=$('#setTel').val();
            $('#informBtn').attr("disabled","disabled")
            if(_this.rechargeData){
                cn.Ajax(api.lianyiRecharge,{
                    mobileNo:telsInput,
                    amount:amounts,
                    smId: _this.rechargeData.retSmId,
                    smCode:smCodes,
                    merchOrderId:_this.rechargeData.retMerchOrderId
                },function(d){
                    $('#informBtn').removeAttr("disabled");
                    cn.alert(d.message)
                    _this.rechargeData=null;
                    if(d.flag==1){
                        window.location.href='/view/payResult.html'
                    }else{
                        window.location.href='/view/payNothing.html'
                    }

                },'post')
            }else {
                cn.alert('请重新获取验证码')
            }
        },
        init:function () {
            const _this=this;
            _this.isLogin();
            _this.bank_list();
            $('#codeTxt').on('click',function () {
                    var txt=$('#codeTxt').text();
                    var telsInput=$('#setTel').val();
                    if(_this.p.istime)return
                    _this.p.istime=true
                    _this.p.time=60;
                    cn.Ajax(api.lianyiCode,{
                        mobileNo : telsInput,
                    },function(d){
                        // console.log(d)
                        if(d.flag==1){
                            _this.rechargeData=d
                            _this.getcodeTime(txt)
                        }else{
                            _this.p.istime=false;
                            cn.alert(d.message)
                        }

                    },'post')

            });
            $('#informBtn').on('click',function () {
                if(!_this.isBindName){
                    cn.modelDialog({
                        msg:'请先实名认证',
                        btnValue:'取消',
                        btn:[{
                            txt:'确定',
                            callBack:function(){
                                window.location.href='/view/real_name.html'
                            }
                        }]
                    });
                }else if(!_this.isBindBank){
                    cn.modelDialog({
                        msg:'请先绑定银行卡',
                        btnValue:'取消',
                        btn:[{
                            txt:'确定',
                            callBack:function(){
                                window.location.href='/view/bind_bank.html'
                            }
                        }]
                    });
                }else{
                    _this.reCharge()
                }
            });



        }

    }
});